import { _decorator, Component, Node, Camera, Label, Tween, tween, v3 } from 'cc';
import { GlobalConfig } from '../../Init/Config/GlobalConfig';
import GlobalData from '../../Init/Config/GlobalData';
import { GlobalEnum } from '../../Init/Config/GlobalEnum';
import { GlobalTmpData } from '../../Init/Config/GlobalTmpData';
import EventManager from '../../Init/Managers/EventManager';
import { EventTypes } from '../../Init/Managers/EventTypes';

const { ccclass, property } = _decorator;
/**僵尸 对话控制 */
@ccclass('LvDialogCmp')
export class LvDialogCmp extends Component {
    cameraUI: Camera = null;
    pos = v3();
    @property(Node)
    dialog: Node = null;
    dialogLabel: Label = null;

    //随机对话参数
    randIndex = -1;
    isRandReady = false;
    showRandCd = 30;
    curRandTime = 0;

    //引导对话参数
    guideIndex = -1;
    //事件对话参数
    eventRecs: { [type: number]: { index: number, curt: number } } = {};
    //同一种类型触发的对话 CD控制
    eventCd = 40;
    //引导记录
    guideRecs: { [state: number]: number } = {};

    //
    animScale = v3();
    //
    isShowing = false;

    onLoad() {
        EventManager.on(EventTypes.RoleEvents.ShowDialog, this.onEventDialog, this);
        EventManager.on(EventTypes.RoleEvents.EmitGuideZombieState, this.onEmitGuideZombieState, this);

        this.dialogLabel = this.dialog.getComponentInChildren(Label);
        this.cameraUI = GlobalData.get(GlobalEnum.GlobalDataType.Camera3D);
    }

    onEnable() {
        this.isShowing = false;
        this.curRandTime = 0;
        this.guideIndex = -1;
        this.randIndex = -1;
        this.eventRecs = {};
        this.guideRecs = {};

        Tween.stopAllByTarget(this.animScale);
        this.animScale.set(0);
        this.dialog.setScale(this.animScale);
    }
    tmpPos = v3();
    update(dt: number) {
        this.randomDialogCheck(dt);

        //同步位置
        if (GlobalTmpData.Game.isGuideLv && !GlobalTmpData.Game.isGuideFinish) {
            this.tmpPos.set(GlobalTmpData.Player.guideLpos3d);
        } else {
            this.tmpPos.set(GlobalTmpData.Player.lpos3D);
        }

        this.tmpPos.y += 2.5;
        this.cameraUI.convertToUINode(this.tmpPos, this.node, this.tmpPos);
        this.dialog.setPosition(this.tmpPos);
        this.dialog.setScale(this.animScale);

        //事件对话Cd控制
        for (const key in this.eventRecs) {
            const rec = this.eventRecs[key];
            if (rec) {
                rec.curt += dt;
            }
        }
    }

    //随机对话
    randomDialogCheck(dt) {
        if (this.isShowing || GlobalTmpData.PartyInfo.isEnter) return;
        //检测僵尸数量与状态
        const tmpData = GlobalTmpData.Player;
        this.isRandReady = (!GlobalTmpData.Game.isGuideLv ||
            GlobalTmpData.Game.isGuideLv && GlobalTmpData.Game.isGuideFinish) && tmpData.curCtrlNum > 0;

        if (this.isRandReady) {
            this.curRandTime += dt;
            if (this.curRandTime >= this.showRandCd) {
                this.showRandCd = Math.floor(Math.random() * 5) + 5;
                this.curRandTime = 0;
                const cfg = GlobalTmpData.Game.isGameRun ? LvRandDialogCfgs : HomeDialogCfgs;

                let arr = [];
                for (let i = 0; i < cfg.length; i++) {
                    if (i != this.randIndex) {
                        arr.push(i);
                    }
                }
                this.randIndex = arr[Math.floor(Math.random() * arr.length)];
                this.showDialog(cfg[this.randIndex]);
            }
        }

    }

    //事件触发对话
    onEventDialog(type: GlobalEnum.DialogTargetType, dialogId?: number) {
        if (!GlobalTmpData.Game.isGameRun) return;
        if (this.isShowing) return;

        //概率触发
        let rand = Math.random();
        // rand = 1;//test
        if (rand < 0.3) return;

        if (!this.eventRecs[type]) {
            this.eventRecs[type] = { index: -1, curt: 0 };
        } else {
            //同种Cd控制
            if (this.eventRecs[type].curt < this.eventCd) return;
            this.eventRecs[type].curt = 0;
        }
        const arr = EventDialogCfgs[type];
        let randStr;
        if (undefined == dialogId) {
            //随机一个id;            
            dialogId = Math.floor(Math.random() * arr.length);
            randStr = arr[dialogId];
        } else {
            randStr = arr[dialogId];
        }
        this.showDialog(randStr);
    }

    //引导关卡 的僵尸对话监听-每一种对话只出现一次
    onEmitGuideZombieState(dialogId: number) {
        if (!this.guideRecs[dialogId]) {
            this.guideRecs[dialogId] = 1;
            let rec = GuideDialogCfgs[dialogId];
            this.showDialog(rec.str, rec.delay);
        }

    }

    //显示
    showDialog(msg: string, delay = 3) {
        this.isShowing = true;
        this.dialogLabel.string = msg;

        //动画
        this.animScale.set(0, 0, 0);
        tween(this.animScale).
            to(0.5, { x: 1, y: 1 }, { easing: 'backOut' }).
            delay(delay).
            to(0.5, { x: 0, y: 0 }, { easing: 'backIn' }).
            call(() => {
                this.isShowing = false;
            }).start();
    }
}
//引导对话
export const GuideDialogCfgs = [
    { str: '哈哈，我终于获得重生啦，我要吃肉肉！', delay: 3 },
    { str: '人类，别想逃！', delay: 4 },
    { str: '前面有人，哈哈，我可以开餐啦！', delay: 5 },
    { str: '吃完肉还能壮大僵尸兵团，真棒！', delay: 3 },
    { str: '咦？还有漏网之鱼，追上去！', delay: 3 },
]
//首页对话
export const HomeDialogCfgs = [
    '嘿，后面的踩着我脚后跟了',
    '点击屏幕，开始捕猎吧！',
    '好饿啊，好想吃新鲜的脑子啊',
    '疼，被僵尸道长扎到我屁屁了',
    '新来那家伙是不是吃大蒜了',
    '喂，谁有假牙借我用用啊',
    '遇到特种兵记得要躲木板后面',
    '注意脚下，别踩着地雷了',
    // '小伙子跑得真快，看来得多锻炼了',
    // '增加多点帮手，僵尸多，力量大嘛',
]
//游戏中随机对话
export const LvRandDialogCfgs = [
    '好饿啊，好想吃新鲜的脑子啊',
    '喂，谁有假牙借我用用啊',
    // '小伙子跑得真快，看来我们得多锻炼了',
    '增加多点帮手，僵尸多，力量大嘛',
]
//游戏中事件触发对话-僵尸
export const EventDialogCfgs = {
    //people
    0: [
        '美味的脑子我来了'
    ],
    //Army
    1: [
        '敌人火力较强，注意找掩体防护!',
    ],
    //TankBoss
    2: [
        '坦克火力太猛,小心点!',
    ],
    //ArmyBoss
    3: [
        '注意BOSS，需消耗对应数量僵尸击杀BOSS!',
    ],
    //mine
    4: [
        '注意地雷预警，被炸到会受伤严重哦',
    ],
    //tips
    5: [
        '跑到安全处，不受攻击使可缓慢回血!',
    ]
}
